<?php

namespace app\award;

use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\ProductArea;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Up
{
    protected $order;
    protected $user;

    protected $set;

    protected $first_order_item;
    protected $amount;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();

        $this->first_order_item = $first_order_item;
        $product_id = $first_order_item->product_id;
        $this->set = SetUserUp::getLastOne();
        $is_bao = ProductArea::checkBao($product_id);

        //  总金额
        $this->amount = $this->order->total_pay - $this->order->total_freight;

        // 套数
        $items = OrderItem::find()
            ->with(['product'])
            ->where(['order_id' => $this->order->id])
            ->all();
        $box = 0;
        foreach ($items as $item) {
            $box += $item['product']['box'] * $item['quantity'];
        }
        $this->order->box = $box;
        $this->order->save();

        // 必须是报单区
        if ($is_bao) {
            $this->upMyself();
            // $this->upChain();
            $this->box();
        }

    }


    // 升级自己
    protected function upMyself()
    {
        // if ($this->first_order_item->price >= $this->set->vip1) {
        //     if ($this->user->level < UserLevel::$keys['vip1']) {
        //         $this->user->level = UserLevel::$keys['vip1'];
        //         $this->user->save();
        //     }
        // }

        // 价格升级
        if ($this->amount >= $this->set->vip1) {
            if ($this->user->level < UserLevel::$keys['vip1']) {
                $this->user->level = UserLevel::$keys['vip1'];
                $this->user->save();
            }
        }

        // 根据套数升级
        if ($this->order->box >= $this->set->vip6) {
            if ($this->user->level < UserLevel::$keys['vip6']) {
                $this->user->level = UserLevel::$keys['vip6'];
                $this->user->save();
            }
        } else if ($this->order->box >= $this->set->vip5) {
            if ($this->user->level < UserLevel::$keys['vip5']) {
                $this->user->level = UserLevel::$keys['vip5'];
                $this->user->save();
            }
        } else if ($this->order->box >= $this->set->vip4) {
            if ($this->user->level < UserLevel::$keys['vip4']) {
                $this->user->level = UserLevel::$keys['vip4'];
                $this->user->save();
            }
        } else if ($this->order->box >= $this->set->vip3) {
            if ($this->user->level < UserLevel::$keys['vip3']) {
                $this->user->level = UserLevel::$keys['vip3'];
                $this->user->save();
            }
        } else if ($this->order->box >= $this->set->vip2) {
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
            }
        }

    }

    // 累计升级
    protected function box()
    {
        $box = (float)Order::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['is_repeat' => 2])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['REFUND']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->sum('box');

        // var_dump($box);exit;

        // 根据套数升级
        if ($box >= $this->set->vip6) {
            if ($this->user->level < UserLevel::$keys['vip6']) {
                $this->user->level = UserLevel::$keys['vip6'];
                $this->user->save();
            }
        } else if ($box >= $this->set->vip5) {
            if ($this->user->level < UserLevel::$keys['vip5']) {
                $this->user->level = UserLevel::$keys['vip5'];
                $this->user->save();
            }
        } else if ($box >= $this->set->vip4) {
            if ($this->user->level < UserLevel::$keys['vip4']) {
                $this->user->level = UserLevel::$keys['vip4'];
                $this->user->save();
            }
        } else if ($box >= $this->set->vip3) {
            if ($this->user->level < UserLevel::$keys['vip3']) {
                $this->user->level = UserLevel::$keys['vip3'];
                $this->user->save();
            }
        } else if ($box >= $this->set->vip2) {
            if ($this->user->level < UserLevel::$keys['vip2']) {
                $this->user->level = UserLevel::$keys['vip2'];
                $this->user->save();
            }
        }
    }

    // 升级关系链
    // protected function upChain()
    // {
    //     // 只需要处理VIP1的
    //     $chain = Chain::getReferrerChain($this->user->id, [
    //         UserLevel::$keys['vip1'],
    //     ], true);
    //
    //     foreach ($chain as $user) {
    //         if ($user->level < UserLevel::$keys['vip2']) {
    //
    //             $count = Chain::getChildrenLevel($user->id, [
    //                 UserLevel::$keys['vip1'],
    //                 UserLevel::$keys['vip2'],
    //             ]);
    //
    //             if (count($count) >= 2) {
    //                 $user->level = UserLevel::$keys['vip2'];
    //                 $user->save();
    //             }
    //
    //         }
    //
    //
    //     }
    // }


}